<?php 
// this file and function checked admin session value stored or not
require_once("../includefiles.php");
require_once("../model/admindetails.php");
require_once ("../includes/classes/pagination_class.php");
require_once '../includes/functions.php'; 
require_once '../model/moduleuser.php';
require_once '../model/message.php'; 
require_once '../model/users.php'; 


$Obj =  new Model_Message();
$userObj =  new Model_Users();	
$moduleObj=new Model_ModuleUser();

 ## delete selected records
/*******************************/
if(isset($_POST['go']) != '' && isset($_POST['action']) != '') {
		$ids = implode(",", $_POST['checkall']);
		## check which action is selected
		$hidden_page = $_POST['hidden_pageno'];	
			## Delete selected records
			$Obj->deleteMultipleMessage($ids);
			$_SESSION['msg']="<div class='success_msg'><span>Message(s) deleted successfully</span></div>";
		
		header('location:'.SITE_URL.'/admin/home.php?q=messages&page='.$hidden_page);
			exit;
		## Delete code will be here if required
		
} // if close
 
if(isset($_GET['action']) == 'delete' && isset($_GET['id']) != '') 
{
	$id = base64_decode($_GET['id']);
	## Delete Enquiry 
	$Obj->deleteMessageValueById($id);
	$_SESSION['msg']="<div class='success_msg'><span>Message deleted successfully</span></div>";
	header('location:'.SITE_URL.'/admin/home.php?q=messages&page='.$_GET['page']);
	exit;
} 
 /*******************************/
if($_GET['q']=='messages')
{
	$moduelArray=$moduleObj->getModuleHelpText(24);
	$smarty->assign('moduelArray', $moduelArray);
}

##  --------- Pagination part first start --------------##
if( isset( $_GET[ 'page' ] ) && $_GET[ 'page' ] != "" ) {
 $pageNum = $_GET[ 'page' ];
} else {
	 $pageNum = 1;
} 
$rowsPerPage = ROW_PER_PAGE;
if($_GET['col1'] != '') 
{
	$pageName = "home.php?q=messages&col1=".$_GET['col1'].""; ;
} 
else
	$pageName = "home.php?q=messages"; 
## Get search parameters in variables - 
if($_POST['search'] != '' && $_POST['search'] != 'Subject') {
$search = addslashes($_POST['search']);
} else if($_GET['search'] != '') {
$search = addslashes($_GET['search']);
} else {
$search = '';
}
$smarty->assign('search', stripslashes (stripslashes ($search)));

if($_POST['fromname'] != '' ) {
$fromname = base64_decode($_POST['fromname']);
} else if($_GET['fromname'] != '') {
$fromname = base64_decode($_GET['fromname']);
} else {
$fromname = '';
}

$smarty->assign('fromname', base64_encode($fromname));

if($_POST['toname'] != '' ) {
$toname = base64_decode($_POST['toname']);
} else if($_GET['toname'] != '') {
$toname = base64_decode($_GET['toname']);
} else {
$toname = '';
}
$smarty->assign('toname', base64_encode($toname));

if($_GET['col1'] != '') 
{
	$col1 = base64_decode($_GET['col1']);
} 

## Count all the records
$msgArray = $Obj->getAllMessage($search,$fromname,$toname,$col1);
$total_rows = count($msgArray);
$offset	= ($pageNum - 1) * $rowsPerPage;
$smarty->assign('page', $pageNum);
##  --------- Pagination part first end --------------##

##  --------- Pagination part second start --------------##
$newid = $pageNum * $rowsPerPage - $rowsPerPage + 1;
$smarty->assign('newid', $newid);
$msgArray = $Obj->getAllMessage($search,$fromname,$toname,$col1,$rowsPerPage, $offset);
$i=0;
foreach($msgArray as $msg)
{
	$msgArray[$i]['from_name'] = $userObj->getUserNameByUserId($msg['from_id']);
	$msgArray[$i]['to_name'] = $userObj->getUserNameByUserId($msg['to_id']);
	$i++;
}
$smarty->assign("msgArray",$msgArray);
$other_id="";
if($search!='' && $search!='Message')
{
	$other_id .= "&search=".$search;
}
if($fromname!='')
{
	$other_id .="&fromname=".base64_encode($fromname);	
}
if($toname!='')
{
	$other_id .="&toname=".base64_encode($toname);	
}
	


if($total_rows > ROW_PER_PAGE) {
	$pg = new pagination();					// CREATING OBJECT OF THE PAGINATION CLASS
	$pgnation = $pg->pagination_admin( $total_rows , $rowsPerPage , $pageNum , $pageName , $other_id, $class);	
	$smarty-> assign("pagination",$pgnation);
}
##  --------- Pagination part second end --------------##	

if($_SESSION['msg']){
	$smarty->assign("msg", $_SESSION['msg']);
	unset($_SESSION['msg']);
}
$userArr = $userObj->getAllActiveUsersWithAdmin();
$smarty->assign("userArr", $userArr);


## Set active class variable for left menu  (main menu and sub menu)
$smarty->assign('activeclass', 'messages');
$smarty->assign('mainmenu', '3');

## Unset all the objects created which are on this page
unset($Obj);
unset($moduleObj);

$smarty->display(TEMPLATEDIR_ADMIN. 'messages/messages.tpl');
unset($smarty);

?>
